package com.example.controller;


import cn.dev33.satoken.annotation.SaCheckRole;
import com.example.service.AiService;
import io.github.pigmesh.ai.deepseek.core.DeepSeekClient;
import io.github.pigmesh.ai.deepseek.core.chat.ChatCompletionRequest;
import io.github.pigmesh.ai.deepseek.core.chat.ChatCompletionResponse;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import reactor.core.publisher.Flux;
import retrofit2.http.Tag;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author zhexueqi
 * @ClassName AiController
 * @since 2025/2/24    2:47
 */
@RestController
@Api(value = "AI相关接口", tags = "AI相关接口")
@RequestMapping("/ai")
public class AiController {

    @Autowired
    private DeepSeekClient deepSeekClient;

    @Resource
    private AiService aiService;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @GetMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ChatCompletionResponse> chat(String prompt) {
        return deepSeekClient.chatFluxCompletion(prompt);
    }

    @GetMapping(value = "/analyze/social-insurance",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @SaCheckRole("bureau")
    public Flux<ChatCompletionResponse> analyzeSocialInsurance(
        @RequestParam String dataType,
        @RequestParam String startDate,
        @RequestParam String endDate,
        @RequestParam String dimensions
    ) {
        String prompt = "请分析以下社保数据集，重点关注：" +
                "1. 参保人数变化趋势及其影响因素" +
                "2. 不同年龄段、性别、地区的参保特点" +
                "3. 未来6个月的参保趋势预测" +
                "4. 对社保政策效果的评估" +
                "数据集：" + "data"+
                "请提供详细的分析报告，包含关键数据支撑和具体建议，请注意，只能按照我要求的返回格式返回给我，返回格式如下：{\n" +
                "  \"analysis_result\": {\n" +
                "    \"summary\": \"基于当前数据集分析，参保人群年龄集中在18-27岁，性别分布均衡，地区数据高度集中。因数据维度有限，预测结果仅供参考\",\n" +
                "    \"demographics\": {\n" +
                "      \"age_distribution\": {\n" +
                "        \"18-20\": 4,\n" +
                "        \"21-23\": 4,\n" +
                "        \"24-27\": 5\n" +
                "      },\n" +
                "      \"gender_ratio\": {\n" +
                "        \"male\": 54.5,\n" +
                "        \"female\": 45.5\n" +
                "      },\n" +
                "      \"region_distribution\": {\n" +
                "        \"阿里云\": 100\n" +
                "      }\n" +
                "    },\n" +
                "    \"time_series\": {\n" +
                "      \"dates\": [\"2025-02\"],\n" +
                "      \"actual_values\": [11],\n" +
                "      \"predictions\": [12]\n" +
                "    },\n" +
                "    \"policy_metrics\": {\n" +
                "      \"coverage_rate\": 100,\n" +
                "      \"new_registrations\": 11\n" +
                "    }\n" +
                "  }\n" +
                "}";
        return aiService.chatWithAi(dataType, startDate, endDate, dimensions,prompt);
    }

    /**
     * 同步数据分析
     */
    @GetMapping("/analyze/sync-social")
    @SaCheckRole("bureau")
    public String syncChat(@RequestParam String dataType,
                                           @RequestParam String startDate,
                                           @RequestParam String endDate,
                                           @RequestParam String dimensions) {
        String caCheKey = "bureau" + dataType + startDate + endDate + dimensions;
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        if (valueOperations.get(caCheKey) != null) {
            return (String) valueOperations.get(caCheKey);
        }
        String prompt = "请分析以下社保数据集，重点关注：" +
                "1. 参保人数变化趋势及其影响因素" +
                "2. 不同年龄段、性别、地区的参保特点" +
                "3. 未来6个月的参保趋势预测" +
                "4. 对社保政策效果的评估" +
                "数据集：" + "data"+
                "请提供详细的分析报告，包含关键数据支撑和具体建议，请注意，只能按照我要求的返回格式返回给我，返回格式如下：{\n" +
                "  \"analysis_result\": {\n" +
                "    \"summary\": \"基于当前数据集分析，参保人群年龄集中在18-27岁，性别分布均衡，地区数据高度集中。因数据维度有限，预测结果仅供参考\",\n" +
                "    \"demographics\": {\n" +
                "      \"age_distribution\": {\n" +
                "        \"18-20\": 4,\n" +
                "        \"21-23\": 4,\n" +
                "        \"24-27\": 5\n" +
                "      },\n" +
                "      \"gender_ratio\": {\n" +
                "        \"male\": 54.5,\n" +
                "        \"female\": 45.5\n" +
                "      },\n" +
                "      \"region_distribution\": {\n" +
                "        \"阿里云\": 100\n" +
                "      }\n" +
                "    },\n" +
                "    \"time_series\": {\n" +
                "      \"dates\": [\"2025-02\"],\n" +
                "      \"actual_values\": [11],\n" +
                "      \"predictions\": [12]\n" +
                "    },\n" +
                "    \"policy_metrics\": {\n" +
                "      \"coverage_rate\": 100,\n" +
                "      \"new_registrations\": 11\n" +
                "    }\n" +
                "  }\n" +
                "}";
        String result = aiService.analyzeAi(dataType, startDate, endDate, dimensions, prompt);
        valueOperations.set(caCheKey, result, 2, TimeUnit.HOURS);
        return result;
    }



}
